﻿

public class Solution49
{
    public IList<IList<string>> GroupAnagrams2(string[] strs)//自己的实现
    {
        IList<IList<string>> res = new List<IList<string>>();
        int listIndex = 0;

        Dictionary<string, int> counts = new Dictionary<string, int>();//string是排序的数组，int是list的index

        for (int i = 0; i < strs.Length; i++)
        {
            //先排序

            string curStr = strs[i];
            string sortStr = MySort(curStr);

            Console.WriteLine("curStr:   " + curStr);
            Console.WriteLine("sortStr:   " + sortStr);
            Console.WriteLine(sortStr);
            if (counts.TryGetValue(sortStr, out int val))
            {
                Console.WriteLine("val:   " + val);
                res[val].Add(curStr);
            }
            else
            {
                res.Add(new List<string>());
                res[listIndex].Add(curStr);
                counts.Add(sortStr, listIndex);
                Console.WriteLine("curStr:   " + sortStr + "   listIndex:  " + listIndex);
                listIndex = listIndex + 1;
            }


        }

        return res;


    }

    public string MySort(string v)
    {

        char[] chars = v.ToCharArray();//转成数组

        //冒泡排序
        for (int j = 0; j < chars.Length - 1; j++)
        {
            for (int i = 0; i < chars.Length - 1 - j; i++)
            {


                int index = chars[i] - 'a';
                int nextindex = chars[i + 1] - 'a';

                if (index > nextindex)
                {

                    swap(chars, i, i + 1);

                }


            }
        }
        string sort = new string(chars);
        //Console.WriteLine(sort);
        return sort;


    }

    private void swap(char[] chars, int i, int v)
    {
        char c = chars[i];
        chars[i] = chars[v];
        chars[v] = c;
    }

    public IList<IList<string>> GroupAnagrams(string[] strs)//使用一些api
    {
        IList<IList<string>> res = new List<IList<string>>();
        

        Dictionary<string, IList<string>> counts = new Dictionary<string, IList<string>>();//string是排序的数组，int是list的index

        for (int i = 0; i < strs.Length; i++)
        {


            string curStr = strs[i];
            char[] chars = curStr.ToCharArray();
            Array.Sort(chars);
            string sortStr = new string(chars);

            Console.WriteLine("curStr:   " + curStr);
            Console.WriteLine("sortStr:   " + sortStr);
            Console.WriteLine(sortStr);
            if (counts.TryGetValue(sortStr, out IList<string> val))
            {
                Console.WriteLine("val:   " + val);
                val.Add(curStr);
            }
            else
            {
                IList<string> strings = new List<string>();
                strings.Add(curStr);
                res.Add(strings);

                counts.Add(sortStr, strings);

            }
        }
        return res;

    }
}

class Program49
{
    static void Main49(string[] args)
    {
        string[] strs = ["eat", "tea", "tan", "ate", "nat", "bat"];
        Solution49 solution = new Solution49();
        string a = solution.MySort("eat");
        string b = solution.MySort("tea");
        if (a == b)
        {
            Console.WriteLine("一样");
        }
        else
        {
            Console.WriteLine("不一样");
            Console.WriteLine(a);
            Console.WriteLine(b);
        }


        IList<IList<string>> res = solution.GroupAnagrams(strs);
        for (int i = 0; i < res.Count; i++)
        {
            for (int j = 0; j < res[i].Count; j++)
            {
                Console.Write(res[i][j] + "   ");
            }

            Console.WriteLine();
        }

        Console.WriteLine("取余"+10%3);
        Console.WriteLine("指数"+Math.Pow(2,10));
    }
}